﻿using System;
using System.Linq;
using System.Windows;
using System.Xml.Linq;
using Microsoft.Phone.Tasks;

namespace Kiva7.RatingReminders
{
    public static class RatingReminder
    {
        public static int PerUseCount { get; set; }
        public static int PerDays { get; set; }
        public static string MessageTitle { get; set; }
        public static string MessageText { get; set; }
        public static ReminderSettings Settings;

        static RatingReminder()
        {
            // setup some defaults...
            PerUseCount = 15;
            PerDays = 14;
            MessageTitle = @"Rate this application?";
            MessageText = @"If you enjoy using {0}, would you mind taking a moment to rate it? It won't take more than a minute. Thank you for your support.";

            // load the settings
            Settings = IsoStore.LoadSettings();

            if (Settings != null)
                return;

            // we have no settings, so this is the first run, setup a new settings class and save it...
            Settings = new ReminderSettings {NextReminderDate = DateTime.Now.AddDays(PerDays)};
            Settings.Save();
        }

        /// <summary>
        /// 
        /// </summary>
        public static void CheckReminder()
        {
            Settings.UsageCount++;
            Settings.Save();

            var showReminder = false;

            if (Settings.NextReminderDate == null)
                return;

            if (Settings.UsageCount == PerUseCount)
                showReminder = true;

            if (Settings.NextReminderDate < DateTime.Now)
                showReminder = true;

            if (!showReminder) return;

            // We're still here, so make sure we show a reminder on the UI thread
            Deployment.Current.Dispatcher.BeginInvoke(ShowReminder);

        }

        /// <summary>
        /// 
        /// </summary>
        private static void ShowReminder()
        {
            if (MessageText.Contains("{0}"))
            {
                MessageText = string.Format(MessageText, GetApplicationTitle());
            }

            ConfirmationBox.Show(MessageTitle, MessageText, RateApp, NeverRateApp, ScheduleReminder);
        }

        /// <summary>
        /// 
        /// </summary>
        private static void ScheduleReminder()
        {
            Settings.NextReminderDate = DateTime.Now.AddDays(PerDays);
            Settings.Save();
        }

        /// <summary>
        /// Set the app to not remind again and take them to the marketplace
        /// </summary>
        private static void RateApp()
        {
            // don't remind them again
            Settings.NextReminderDate = null;
            Settings.Save();

            // take them to the marketplace for review
            var marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
        }

        /// <summary>
        /// Set the app to not remind again
        /// </summary>
        private static void NeverRateApp()
        {
            // don't remind them again
            Settings.NextReminderDate = null;
            Settings.Save();
        }

        /// <summary>
        /// Get's the Title attribute for the application
        /// </summary>
        /// <returns></returns>
        private static string GetApplicationTitle()
        {
            var ret = string.Empty;

            var xe = XElement.Load("WMAppManifest.xml");
            var attr = (from manifest in xe.Descendants("App")
                        select manifest).SingleOrDefault();
            if (attr != null)
            {
                var xAttribute = attr.Attribute("Title");
                if (xAttribute != null) ret = xAttribute.Value;
            }

            return ret;
        }
    }
}
